package com.yiqi.news.mvp.presenter

import android.app.Application

import com.jess.arms.integration.AppManager
import com.jess.arms.di.scope.ActivityScope
import com.jess.arms.mvp.BasePresenter
import com.jess.arms.http.imageloader.ImageLoader
import com.jess.arms.utils.LogUtils
import com.yiqi.huize.isNull
import com.yiqi.news.app.service.UserController
import com.yiqi.news.app.utils.RxUtils
import com.yiqi.news.entity.*
import me.jessyan.rxerrorhandler.core.RxErrorHandler
import javax.inject.Inject

import com.yiqi.news.mvp.contract.NewsDetailContract
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 06/19/2019 18:01
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
class NewsDetailPresenter
@Inject
constructor(model: NewsDetailContract.Model, rootView: NewsDetailContract.View) :
        BasePresenter<NewsDetailContract.Model, NewsDetailContract.View>(model, rootView) {
    @Inject
    lateinit var mErrorHandler: RxErrorHandler
    @Inject
    lateinit var mApplication: Application
    @Inject
    lateinit var mImageLoader: ImageLoader
    @Inject
    lateinit var mAppManager: AppManager

    override fun onDestroy() {
        super.onDestroy();
    }


    fun getNewsData(newsId: String, isFirst: Boolean = true) {
        if (newsId.isNullOrEmpty()) return
        mModel.getNewsDetail(newsId, if (UserController.getInstance().isLogin) UserController.getInstance().loginResultCache.userId else "0")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtils.applySchedulers(mRootView))
                .subscribe(object : ErrorHandleSubscriber<BaseResponse<NewsDetail>>(mErrorHandler) {
                    override fun onNext(t: BaseResponse<NewsDetail>) {

                        if (t.isSuccess && !t.data.isNull()) {
                            if (isFirst)
                                mRootView.showNewsDetail(t.data)
                            else
                                mRootView.showNewsRefresh(t.data)

                        } else {
                            mRootView.showNewsError(t.msg)
                        }

                    }

                })
    }


    fun doCommentNews(materialId: String, materialType: Int, commentInfo: String) {
        if (commentInfo.isNullOrEmpty()) {
            mRootView.showError("请输入评论类容")
            return
        }
        var map = HashMap<String, Any?>()
        map["materialId"] = materialId
        map["materialType"] = materialType
        map["commentInfo"] = commentInfo
        mModel.toComment(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtils.applySchedulers(mRootView))
                .subscribe(object : ErrorHandleSubscriber<BaseResponse<NewsDetail>>(mErrorHandler) {

                    override fun onNext(t: BaseResponse<NewsDetail>) {
                        if (t.isSuccess) {
                            mRootView.showCommentSucess()
                        } else {
                            mRootView.showError(t.msg)
                        }
                    }
                })
    }

    /**
     * 新闻点赞
     * */
    fun doLikeNews(type: Int, typeId: String) {
//        type (integer): 点赞类型 1、作品点赞 2、 评论点赞 ,3、对回复点赞
//        typeId (integer): 对应的素材作品或评论的id
        var map = HashMap<String, Any?>()
        map["type"] = type
        map["typeId"] = typeId
        mModel.toLike(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtils.applySchedulers(mRootView))
                .subscribe(object : ErrorHandleSubscriber<BaseResponse<NewsDetail>>(mErrorHandler) {

                    override fun onNext(t: BaseResponse<NewsDetail>) {
                        if (t.isSuccess) {
                            mRootView.showLikeSucess(type)
                        } else {
                            mRootView.showError(t.msg)
                        }
                    }
                })
    }

    fun requestReplyMessage(commentId: String, content: String, toUserid: Int) {
        var map = HashMap<String, String>()
        map["commentId"] = commentId
        map["content"] = content
        map["replyType"] = "1"
        map["toUserid"] = toUserid.toString()
        mModel.replyMessage(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtils.applySchedulers(mRootView))
                .subscribe(object : ErrorHandleSubscriber<BaseResponse<NewsDetail>>(mErrorHandler) {
                    override fun onNext(t: BaseResponse<NewsDetail>) {
                        if (t.isSuccess) {
                            mRootView.showMessage("回复成功")
                        } else {
                            mRootView.showError(t.msg)
                        }
                    }
                })
    }

    /**
     * 评论点赞
     * */
    fun doLikeComment(type: Int, typeId: String, position: Int) {
//        type (integer): 点赞类型 1、作品点赞 2、 评论点赞 ,
//        typeId (integer): 对应的素材作品或评论的id
        var map = HashMap<String, Any?>()
        map["type"] = type
        map["typeId"] = typeId
        mModel.toLike(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtils.applySchedulers(mRootView))
                .subscribe(object : ErrorHandleSubscriber<BaseResponse<NewsDetail>>(mErrorHandler) {

                    override fun onNext(t: BaseResponse<NewsDetail>) {
                        if (t.isSuccess) {
                            mRootView.showLikeCommentSucess(position)
                        } else {
                            mRootView.showError(t.msg)
                        }
                    }
                })
    }

    /**
     * 回复点赞
     * */
    fun doLikeDialogComment(type: Int, typeId: String, position: Int) {
//        type (integer): 点赞类型 1、作品点赞 2、 评论点赞 ,
//        typeId (integer): 对应的素材作品或评论的id
        var map = HashMap<String, Any?>()
        map["type"] = type
        map["typeId"] = typeId
        mModel.toLike(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtils.applySchedulers(mRootView))
                .subscribe(object : ErrorHandleSubscriber<BaseResponse<NewsDetail>>(mErrorHandler) {

                    override fun onNext(t: BaseResponse<NewsDetail>) {
                        if (t.isSuccess) {
                            mRootView.showLikeDialogCommentSucess(position)
                        } else {
                            mRootView.showError(t.msg)
                        }
                    }
                })
    }

    fun doNewsCollection(materialId: String) {
        var map = HashMap<String, Any?>()
        map["materialId"] = materialId
        mModel.toCollection(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtils.applySchedulers(mRootView))
                .subscribe(object : ErrorHandleSubscriber<BaseResponse<NewsDetail>>(mErrorHandler) {

                    override fun onNext(t: BaseResponse<NewsDetail>) {
                        if (t.isSuccess) {
                            mRootView.showCollectionSucess()
                        } else {
                            mRootView.showError(t.msg)
                        }
                    }
                })
    }
}

